﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace ZhimeiSoft.Framework.Client.Control.DateTimeGrid
{
    
    public partial class MonthDateTimeForWeek : UserControl, IDateTimeView
    {
        public MonthDateTimeForDay currDateTimeViewControl = null;
        public EventDBClickCurrDay OnEventDBClickCurrDay = null;
        public EventClickCurrDay OnEventClickCurrDay = null;

        private MonthDateTimeForDay[] arrPanDateOne = null;
        private MonthDateTimeForDay[] arrPanDateTwo = null;
        private MonthDateTimeForDay[] arrPanDateThree = null;
        private MonthDateTimeForDay[] arrPanDateFour = null;
        private MonthDateTimeForDay[] arrPandateFive = null;
        private MonthDateTimeForDay[] arrPandateSix = null;
        private MonthDateTimeForDay[] arrMonth = null;//天的集合

        private IDateTimeView currDateTimeView;

        public MonthDateTimeForWeek()
        {
            InitializeComponent();
            arrPanDateOne = new MonthDateTimeForDay[] { dateTimeForDay17, dateTimeForDay11, dateTimeForDay12, dateTimeForDay13, dateTimeForDay14, dateTimeForDay15, dateTimeForDay16 };
            arrPanDateTwo = new MonthDateTimeForDay[] { dateTimeForDay27, dateTimeForDay21, dateTimeForDay22, dateTimeForDay23, dateTimeForDay24, dateTimeForDay25, dateTimeForDay26 };
            arrPanDateThree = new MonthDateTimeForDay[] { dateTimeForDay37, dateTimeForDay31, dateTimeForDay32, dateTimeForDay33, dateTimeForDay34, dateTimeForDay35, dateTimeForDay36 };
            arrPanDateFour = new MonthDateTimeForDay[] { dateTimeForDay47, dateTimeForDay41, dateTimeForDay42, dateTimeForDay43, dateTimeForDay44, dateTimeForDay45, dateTimeForDay46 };
            arrPandateFive = new MonthDateTimeForDay[] { dateTimeForDay57, dateTimeForDay51, dateTimeForDay52, dateTimeForDay53, dateTimeForDay54, dateTimeForDay55, dateTimeForDay56 };
            arrPandateSix = new MonthDateTimeForDay[] { dateTimeForDay67, dateTimeForDay61, dateTimeForDay62, dateTimeForDay63, dateTimeForDay64, dateTimeForDay65, dateTimeForDay66 };
            arrMonth = new MonthDateTimeForDay[]{ 
                        dateTimeForDay17, dateTimeForDay11, dateTimeForDay12, dateTimeForDay13, dateTimeForDay14, dateTimeForDay15, dateTimeForDay16,
                        dateTimeForDay27, dateTimeForDay21, dateTimeForDay22, dateTimeForDay23, dateTimeForDay24, dateTimeForDay25, dateTimeForDay26,
                        dateTimeForDay37, dateTimeForDay31, dateTimeForDay32, dateTimeForDay33, dateTimeForDay34, dateTimeForDay35, dateTimeForDay36,
                        dateTimeForDay47, dateTimeForDay41, dateTimeForDay42, dateTimeForDay43, dateTimeForDay44, dateTimeForDay45, dateTimeForDay46,
                        dateTimeForDay57, dateTimeForDay51, dateTimeForDay52, dateTimeForDay53, dateTimeForDay54, dateTimeForDay55, dateTimeForDay56,
                        dateTimeForDay67, dateTimeForDay61, dateTimeForDay62, dateTimeForDay63, dateTimeForDay64, dateTimeForDay65, dateTimeForDay66
            };
            //初始化事件
            IntDateTimeFoorDayClick(arrPanDateOne);
            IntDateTimeFoorDayClick(arrPanDateTwo);
            IntDateTimeFoorDayClick(arrPanDateThree);
            IntDateTimeFoorDayClick(arrPanDateFour);
            IntDateTimeFoorDayClick(arrPandateFive);
            IntDateTimeFoorDayClick(arrPandateSix);
            currDateTimeViewControl = dateTimeForDay17;
            currDateTimeView = (IDateTimeView)dateTimeForDay17;

        }
        /// <summary>
        /// 获取当前控件的开始时间
        /// </summary>
        /// <returns></returns>
        public System.DateTime GetMonthDateStartTime()
        {
            return dateTimeForDay17.GetDateTime();
        }
        /// <summary>
        /// 获取当前控件的结束时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetMonthDateEndTime()
        {
            return dateTimeForDay66.GetDateTime();
        }

        #region IDateTimeView 成员

        public DateTime GetDateTime()
        {
            if (currDateTimeView == null)
            {
                return DateTime.Now;
            }

            return currDateTimeView.GetDateTime();
        }

        public void SetDateTime(DateTime dateTime)
        {
            //IntWeekDate();

            int iMaxDays = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);

            Stack<MonthDateTimeForDay[]> stackWeek = new Stack<MonthDateTimeForDay[]>();
            stackWeek.Push(arrPandateSix);
            stackWeek.Push(arrPandateFive);
            stackWeek.Push(arrPanDateFour);
            stackWeek.Push(arrPanDateThree);
            stackWeek.Push(arrPanDateTwo);
            stackWeek.Push(arrPanDateOne);

            MonthDateTimeForDay[] currWeek = arrPanDateOne;
            DateTime dtNow = new DateTime(dateTime.Year, dateTime.Month, 1);
            if (dtNow.DayOfWeek != DayOfWeek.Sunday)
            {
                dtNow = dtNow.AddDays(0 - Convert.ToInt32(dtNow.DayOfWeek.ToString("d")));
            }

            while (stackWeek.Count != 0)
            {
                currWeek = stackWeek.Pop();
                for (int i = 0; i < currWeek.Length; i++)
                {
                    currWeek[i].SetDateTime(dtNow);
                    currWeek[i].SetEffectiveColor(dtNow.Month == dateTime.Month);
                    if (dtNow.Year == dateTime.Year && dtNow.Month == dateTime.Month && dtNow.Day == dateTime.Day)
                    {
                        this.currDateTimeView = currWeek[i];
                        this.currDateTimeViewControl = currWeek[i];
                    }
                    dtNow = dtNow.AddDays(1);
                }
            }
        }


        public int GetItemID()
        {
            if (currDateTimeView == null)
            {
                return -1;
            }
            return currDateTimeView.GetItemID();
        }

        public void DelItem(int indexID)
        {
            if (currDateTimeView == null)
            {
                return;
            }
            currDateTimeView.DelItem(indexID);
        }

        public void InitDate(DateTime dtInit)
        {
            if (currDateTimeView == null)
            {
                return;
            }
            currDateTimeView.InitDate(dtInit);
        }
        
        public void SetEffectiveColor(bool bEffective)
        {
            if (currDateTimeView == null)
            {
                return;
            }
            currDateTimeView.SetEffectiveColor(bEffective);
        }
        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public DataRow GetCurrDataRow()
        {
            if (currDateTimeView == null)
            {
                return null;
            }
            return currDateTimeView.GetCurrDataRow();
        }
        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public object GetCurrValue()
        {
            if (currDateTimeView == null)
            {
                return null;
            }
            return currDateTimeView.GetCurrValue();
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="dsData">绑定</param>
        public void SetDataBind(DataSet dsData, String displayFieldKey, string valueFieldKey, string timeKey)
        {
            DataSet dsBind = dsData.Clone();
            foreach (DataRow drData in dsData.Tables[0].Rows)
            {
                DateTime dataTime = Convert.ToDateTime(drData[timeKey]);
                MonthDateTimeForDay day = GetDayControl(dataTime);
                if (day == null)
                    continue;

                dsBind.Tables[0].ImportRow(drData);
                day.SetDataBind(dsBind, displayFieldKey, valueFieldKey, timeKey);
                dsBind.Tables[0].Rows.Clear();
            }
        }

        #endregion

        /// <summary>
        /// 根据时间返回控件
        /// </summary>
        /// <param name="findDateTime"></param>
        /// <returns></returns>
        public MonthDateTimeForDay GetDayControl(DateTime findDateTime)
        {
            //日历控件的开始时间
            DateTime dtStartDay = dateTimeForDay17.GetDateTime();
            TimeSpan a = findDateTime - dtStartDay;
            if (a.Days < 0 || a.Days > arrMonth.Length)
            {
                return null;
            }

            return arrMonth[a.Days];
        }

        public void ClearBindData()
        {
            ClearDayData(arrPanDateOne);
            ClearDayData(arrPanDateTwo);
            ClearDayData(arrPanDateThree);
            ClearDayData(arrPanDateFour);
            ClearDayData(arrPandateFive);
            ClearDayData(arrPandateSix);
        }

        private void ClearDayData(MonthDateTimeForDay[] arrDay)
        {
            foreach (MonthDateTimeForDay day in arrDay)
            {
                day.ClearBindData();
            }
        }

        private void IntWeekDate()
        {
            InitDateTimeForDay(arrPanDateOne);
            InitDateTimeForDay(arrPanDateTwo);
            InitDateTimeForDay(arrPanDateThree);
            InitDateTimeForDay(arrPanDateFour);
            InitDateTimeForDay(arrPandateFive);
            InitDateTimeForDay(arrPandateSix);
        }
        private void InitDateTimeForDay(MonthDateTimeForDay[] arrDay)
        {
            foreach (MonthDateTimeForDay day in arrDay)
            {
                day.InitDate(DateTime.Now);
            }
        }

        private void IntDateTimeFoorDayClick(MonthDateTimeForDay[] arrDay)
        {
            foreach (MonthDateTimeForDay day in arrDay)
            {
                day.OnClickCurrDay += DateTimeForDay_Click;
                day.OnDBClickCurrDay += DateTimeForDay_DBClick;
            }
        }

        private void DateTimeForDay_Click(object sender)
        {
            currDateTimeView = (IDateTimeView)sender;
            currDateTimeViewControl = (MonthDateTimeForDay)sender;
            UnSelectedItem(sender);
            if (OnEventClickCurrDay != null)
            {
                OnEventClickCurrDay(sender);
            }
        }

        private void DateTimeForDay_DBClick(object sender)
        {
            if (OnEventDBClickCurrDay != null)
            {
                OnEventDBClickCurrDay(sender);
                UnSelectedItem(sender);
            }
        }

        private void UnSelectedItem(object sender)
        {
            if (sender == null)
            {
                return;
            }

            foreach (MonthDateTimeForDay day in arrMonth)
            {
                if (day.GetDateTime().Equals(((MonthDateTimeForDay)sender).GetDateTime()))
                {
                    continue;
                }
                day.UnSelectItem();
            }

        }

        private void MonthDateTimeForWeek_Load(object sender, EventArgs e)
        {
        }
    }

}
